package com.zl.learning.collection;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class TestArrayList {
    @Test
    public void test1() {
        List<String> list = new ArrayList<>(Arrays.asList("1","2","3"));
        final ListIterator<String> iterator = list.listIterator();
        while (iterator.hasNext()){
            final String next = iterator.next();
            System.out.println(next);
            if("1".equals(next)){
                iterator.add("4");
                iterator.previous();
            }
        }

    }

    @Test
    public void test2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<String> list = new ArrayList<>();
        int i = 1;
//        List list1 = list;
//        list1.add(i);
//        System.out.println(list.get(0));
        Class<List> listClass = List.class;
        Method add = listClass.getMethod("add", Object.class);
        add.invoke(list,i);
        System.out.println(list.get(0));
    }

    @Test
    public void test3() {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
//        for (String s : list) {
//            if(s.equals("b")){
//                list.add("e");
//            }
//        }
        final ListIterator<String> iterator = list.listIterator();
        while (iterator.hasNext()){
            final String next = iterator.next();
            if(next.equals("b")){
                iterator.add("E");
            }
        }
        System.out.println(list);
    }
}
